﻿
using QFramework;
using System.Collections.Generic;
using UnityEngine;

namespace ProjectChildhood
{
    public interface IToolBarSystem : QFramework.IToolBarSystem
    {
        List<ToolbarSlot> Slots { get; }

        void LoadData();
        void ResetData();
        void SaveData();
    }

    /// <summary>
    /// 工具栏系统
    /// </summary>
    public class ToolBarSystem : AbstractSystem, IToolBarSystem
    {
        public static EasyEvent<ToolbarSlot, int> OnItemCountChanged = new EasyEvent<ToolbarSlot, int>(); // 工具栏物体数量有变更时触发

        public List<ToolbarSlot> Slots { get; } = new List<ToolbarSlot>()
        {
            new ToolbarSlot("hand", 1),
            new ToolbarSlot("shovel", 1),
            new ToolbarSlot("waterscan", 1),
            //new ToolbarSlot("seed", 2),
            //new ToolbarSlot("seed_carrot", 2),
            new ToolbarSlot("seed_pumpkin", 2),
            new ToolbarSlot("seed_potato", 2),
            new ToolbarSlot("seed_tomato", 2),
            new ToolbarSlot("seed_bean", 2),
            new ToolbarSlot(),
            new ToolbarSlot(),
            new ToolbarSlot(),
        };

        private readonly List<ToolbarSlot> m_InitSlotsConfig = new List<ToolbarSlot>()
        {
            new ToolbarSlot("hand", 1),
            new ToolbarSlot("shovel", 1),
            new ToolbarSlot("waterscan", 1),
            //new ToolbarSlot("seed", 2),
            //new ToolbarSlot("seed_carrot", 2),
            new ToolbarSlot("seed_pumpkin", 2),
            new ToolbarSlot("seed_potato", 2),
            new ToolbarSlot("seed_tomato", 2),
            new ToolbarSlot("seed_bean", 2),
            new ToolbarSlot(),
            new ToolbarSlot(),
            new ToolbarSlot(),
        };

        public void LoadData()
        {
            for (int i = 0; i < Slots.Count; i++)
            {
                Slots[i].ItemName = PlayerPrefs.GetString($"toolbar_slot_{i}_item_id", m_InitSlotsConfig[i].ItemName) ;
                Slots[i].Count.Value = PlayerPrefs.GetInt($"toolbar_slot_{i}_count", m_InitSlotsConfig[i].Count.Value);
            }
        }

        public void ResetData()
        {
            for (int i = 0;i < Slots.Count;i++)
            {
                ToolbarSlot slot = Slots[i];
                slot.ItemName = m_InitSlotsConfig[i].ItemName;
                slot.Count.Value = m_InitSlotsConfig[i].Count;
            }

            SaveData();
        }

        public void SaveData()
        {
            for (int i = 0;i < Slots.Count;i++)
            {
                PlayerPrefs.SetString($"toolbar_slot_{i}_item_id", Slots[i].ItemName);
                PlayerPrefs.SetInt($"toolbar_slot_{i}_count", Slots[i].Count);
            }
        }

        protected override void OnInit()
        {
            
        }
    }
}
